//
//  TKYChangeStationHouseController.m
//  StationHouseHelper
//
//  Created by apple on 2018/6/13.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYChangeStationHouseController.h"
#import "TKYChangeStationHeaderView.h"
#import "TKYChangeStationFootView.h"
#import "TKYSegmentView.h"
#import "TKYLoginController.h"
#import "TKYSettingController.h"
#import "TKYNavigetionController.h"
#import "TKYEdittingDataController.h"
#import "TKYChangeStationHouseCell.h"
#import "TKYProjectInfoModel.h"
#define kChangeStationHouseCellID @"TKYChangeStationHouseCell"
@interface TKYChangeStationHouseController ()<UITableViewDelegate,UITableViewDataSource>

/** 站房 */
@property (nonatomic, strong)BaseTabelView *stationHouseTabelView;

@property (nonatomic, strong)TKYSegmentView *segmentView;


@property (nonatomic, strong)TKYChangeStationHeaderView *changeStationHeaderView;
@property (nonatomic, strong)TKYChangeStationFootView *changeStationFootView ;


/** 标题栏 */
@property (nonatomic, weak) UIView *titlesView;
/** 当前选中的标题按钮 */
@property (nonatomic, strong) UIButton *selectedTitleButton;
/** 标题按钮底部的指示器 */
@property (nonatomic, weak) UIView *indicatorView;


@property (nonatomic, strong)NSArray <TKYProjectInfoModel *>*projectArr;

@end

@implementation TKYChangeStationHouseController


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    CGRect rect = self.view.frame;
    switch (_drawerType) {
        case DrawerDefaultLeft:
            [self.view.superview sendSubviewToBack:self.view];
            break;
        case DrawerTypeMaskLeft:
            rect.size.width = kScreenWidth * 0.8;
            break;
        default:
            break;
    }
    self.view.frame = rect;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //设置界面
    [self setUpUI];
    
    [self requestProjectList];
    
}

/**
 设置界面
 */
- (void)setUpUI{
    //self.view.backgroundColor = m
    [self.view addSubview:self.stationHouseTabelView];
    [self.view addSubview: self.changeStationHeaderView];
    [self.view addSubview: self.changeStationFootView];
    [self.view addSubview:self.segmentView];

}

#pragma mark-- 获取站房信息
- (void)requestProjectList{
    NSString *url = [NSString stringWithFormat:@"%@%@userId=%@&token=%@",kApiBaseUrl, KGetProjectListUrl,[[NSUserDefaults standardUserDefaults]objectForKey:kUserID],[[NSUserDefaults standardUserDefaults]objectForKey:kToken]];
    
    [SVProgressHUD show];
    [KHttpRequestTool GET:url parameters:nil success:^(id response) {
        if ([response[@"code"] integerValue] == 0) {
            self.projectArr = [TKYProjectInfoModel  mj_objectArrayWithKeyValuesArray:response[@"projectList"]];
            [SVProgressHUD dismiss];
        }else{
            [SVProgressHUD showErrorWithStatus:response[@"msg"]];
        }
        [self.stationHouseTabelView reloadData];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}

#pragma mark--UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.projectArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TKYChangeStationHouseCell *cell = [tableView dequeueReusableCellWithIdentifier:kChangeStationHouseCellID];
    cell.projectInfoModel = self.projectArr[indexPath.row];
        return cell;
  
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.changeStationHouseBlock(self.projectArr[indexPath.row]);
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark-- lazy
- (BaseTabelView *)stationHouseTabelView{
    if (_stationHouseTabelView == nil) {
        _stationHouseTabelView = [[BaseTabelView alloc]initWithFrame:CGRectMake(0, self.changeStationHeaderView.kx_height +10 +self.titlesView.kx_height +self.segmentView.kx_height , kScreenWidth *0.8, kScreenHeight-self.changeStationHeaderView.kx_height +10 - self.titlesView.kx_height -100) style:UITableViewStylePlain];
        _stationHouseTabelView.dataSource = self;
        _stationHouseTabelView.delegate = self;
        [_stationHouseTabelView registerClass:[TKYChangeStationHouseCell class] forCellReuseIdentifier:kChangeStationHouseCellID];
        _stationHouseTabelView.tableFooterView = [[UIView alloc]init];
        _stationHouseTabelView.mj_header = [TKYRefreshHeader headerWithRefreshingBlock:^{
            
            [_stationHouseTabelView.mj_header endRefreshing];
        }];
        _stationHouseTabelView.mj_footer = [TKYRefreshFooter footerWithRefreshingBlock:^{
            
             [_stationHouseTabelView.mj_footer endRefreshing];
        }];
    }
    return _stationHouseTabelView;
}

- (TKYChangeStationHeaderView *)changeStationHeaderView{
    if (_changeStationHeaderView == nil) {
        _changeStationHeaderView = [[TKYChangeStationHeaderView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth *0.8, kHeightScale *110)];
        __weak typeof(self) weakSelf = self;
        _changeStationHeaderView.editDataButtonBlock = ^(UIButton *button) {
            TKYEdittingDataController *edittingVC = [[TKYEdittingDataController alloc]init];
            TKYEdittingDataController *nav = [[TKYNavigetionController alloc]initWithRootViewController:edittingVC];
            
            [weakSelf presentViewController:nav animated:YES completion:nil];
            NSLog(@"编辑资料");
        };
        
    }
    return _changeStationHeaderView;
}

- (TKYChangeStationFootView *)changeStationFootView{
    if (_changeStationFootView == nil) {
        _changeStationFootView = [[TKYChangeStationFootView alloc]initWithFrame:CGRectMake(0, self.stationHouseTabelView.kx_bottom, kScreenWidth *0.8, kHeightScale *55)];
        __weak typeof(self) weakSelf = self;
        //设置按钮
        _changeStationFootView.setingButtonBlock = ^(UIButton *button) {
            
            TKYSettingController *settingVc = [[TKYSettingController alloc]init];
            TKYSettingController *nav = [[TKYNavigetionController alloc]initWithRootViewController:settingVc];
           
            [weakSelf presentViewController:nav animated:YES completion:nil];
            
        };
        //退出按钮
        _changeStationFootView.logOutButtonBlock = ^(UIButton *button) {
            NSLog(@"点击退出按钮");
            [weakSelf loginOutCurrentAccount];
        };
    }
    return _changeStationFootView;
}

- (TKYSegmentView *)segmentView{
    if (_segmentView == nil) {
        _segmentView = [[TKYSegmentView alloc]initWithFrame:CGRectMake(0, self.changeStationHeaderView.kx_bottom , kScreenWidth, 65)];
    }
    return _segmentView;
}


// 退出当前账号
- (void)loginOutCurrentAccount {
    //添加切换动画
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];  // 获得根窗口
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您确定要退出登录吗?" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [UIView animateWithDuration:0.5 animations:^{
            TKYLoginController *loginVC = [[TKYLoginController alloc] init];//将登录控制器设置为window的根控制器
            [self.view removeFromSuperview];
            window.rootViewController = loginVC;
        }];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
